package com.enjoyor.soa.traffic.core.tpcms.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.api.dto.tpcms.ApplyZoneDto;
import com.enjoyor.soa.traffic.api.dto.tpcms.InfoLimitPeriodDto;
import com.enjoyor.soa.traffic.core.tpcms.domain.InfoAlarmRedisAll;
import com.enjoyor.soa.traffic.core.tpcms.domain.InfoLimitPeriod;
import com.enjoyor.soa.traffic.core.tpcms.domain.InfoZone;
import com.enjoyor.soa.traffic.core.tpcms.domain.PassCard;
import com.enjoyor.soa.traffic.core.tpcms.mapper.IInfoAlarmRedisMapper;
import com.enjoyor.soa.traffic.core.tpcms.mapper.IInfoLimitPeriodMapper;
import com.enjoyor.soa.traffic.core.tpcms.mapper.IInfoZoneMapper;
import com.enjoyor.soa.traffic.core.tpcms.mapper.IPassCardMapper;
import com.enjoyor.soa.traffic.core.tpcms.service.IApplyZoneService;
import com.enjoyor.soa.traffic.core.tpcms.service.IRltBayonetZoneService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.frame.support.redis.IRedisTool;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;

/**
 * 申请区域操作
 * 
 * @author zsy 2018年4月27日
 *
 */
@Service
@Transactional
public class ApplyZoneService implements IApplyZoneService {

    @Autowired
    private IInfoZoneMapper infoZoneMapper;

    @Autowired
    private IInfoLimitPeriodMapper infoLimitPeriodMapper;

    @Autowired
    private IPubLogMapper pubLogMapper;

    @Autowired
    private IInfoAlarmRedisMapper infoAlarmRedisMapper;

    @Autowired
    private IRedisTool redisTool;

    @Autowired
    private IPassCardMapper passCardMapper;

    @Autowired
    private IRltBayonetZoneService rltBayonetZoneService;

    @Value("${spring.redis.noPassKey}")
    private String noPassKey;

    @Value("${log.system.name}")
    private String systemName;

    /**
     * 获取分页信息
     */
    @Override
    @SuppressWarnings("rawtypes")
    public Page getApplyZonePage(Page page) {
        return infoZoneMapper.selectPageInfo(page);
    }

    /**
     * 获取区域信息列表
     */
    @Override
    @SuppressWarnings("rawtypes")
    public List<ApplyZoneDto> getApplyZoneList(Map map) {
        List<ApplyZoneDto> applyZoneDtos = new ArrayList<ApplyZoneDto>();
        List<InfoZone> infoZoneList = infoZoneMapper.selectListInfo(map);
        for (InfoZone infoZone : infoZoneList) {
            ApplyZoneDto applyZoneDto = ReflectHelper.beanToBean(infoZone, ApplyZoneDto.class);
            List<InfoLimitPeriod> limitPeriodList = infoLimitPeriodMapper.getLimitPeriodList(infoZone.getApplyZoneId());
            applyZoneDto.setLimitPeriods(ReflectHelper.listToList(limitPeriodList, InfoLimitPeriodDto.class));
            applyZoneDtos.add(applyZoneDto);
        }
        return applyZoneDtos;
    }

    /**
     * 新增申请区域
     */
    @Override
    public void insertApplyZone(ApplyZoneDto applyZoneDto, String userName) {
        // 完善基础信息
        applyZoneDto.setApplyZoneId("Z" + TimeHelper.getUniqueString());
        applyZoneDto.setIsDelete("0");
        if (applyZoneDto.getIsUse() != "0") {
            applyZoneDto.setId("1");
        }
        applyZoneDto.setUpdateTime(TimeHelper.dateToLongString());
        String applyZoneId = applyZoneDto.getApplyZoneId();
        InfoZone infoZonePojo = ReflectHelper.beanToBean(applyZoneDto, InfoZone.class);
        // 新增区域基础信息表
        infoZoneMapper.insert(infoZonePojo);
        pubLogMapper.saveNoramlLogInfo(userName, systemName, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                String.format("添加InfoZone表单编号:{%s} 操作成功!", new Object[] { infoZonePojo.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.APPLY_REDION_MANAGEMENT.getMessage());
        // 新增限时时段列表
        List<InfoLimitPeriod> limitPeriods = ReflectHelper.listToList(applyZoneDto.getLimitPeriods(),
                InfoLimitPeriod.class);
        for (InfoLimitPeriod infoLimitPeriod : limitPeriods) {
            infoLimitPeriod.setApplyZoneId(applyZoneId);
            infoLimitPeriodMapper.insert(infoLimitPeriod);
        }
    }

    /**
     * 修改区域信息
     */
    @Override
    public void editApplyZone(ApplyZoneDto applyZoneDto, String userName) {
        applyZoneDto.setIsDelete("0");
        applyZoneDto.setUpdateTime(TimeHelper.dateToLongString());
        InfoZone infoZonePojo = ReflectHelper.beanToBean(applyZoneDto, InfoZone.class);
        // 修改区域基础信息表
        infoZoneMapper.updateById(infoZonePojo);
        pubLogMapper.saveNoramlLogInfo(userName, systemName, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("修改InfoZone表单编号:{%s} 操作成功!", new Object[] { infoZonePojo.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.APPLY_REDION_MANAGEMENT.getMessage());
        // 删除限时时段表
        infoLimitPeriodMapper.deleteLimitPeriod(infoZonePojo.getApplyZoneId());
        // 新增限时时段列表
        List<InfoLimitPeriod> limitPeriods = ReflectHelper.listToList(applyZoneDto.getLimitPeriods(),
                InfoLimitPeriod.class);
        for (InfoLimitPeriod infoLimitPeriod : limitPeriods) {
            infoLimitPeriod.setApplyZoneId(infoZonePojo.getApplyZoneId());
            infoLimitPeriodMapper.insert(infoLimitPeriod);
        }
        // 更新无通行证redis
        List<InfoAlarmRedisAll> alarmRedisAll = infoAlarmRedisMapper.getAlarmRedisAll();
        redisTool.setList(noPassKey, alarmRedisAll);
        // 更新所有通行证redis
        List<PassCard> passTrucksList = passCardMapper.getAllPassTrucks();
        rltBayonetZoneService.setRedisByCardList(passTrucksList);
    }

    /**
     * 删除区域信息
     */
    @Override
    public void deleteApplyZone(String applyZoneId, String userName) {
        InfoZone infoZone = new InfoZone();
        infoZone.setApplyZoneId(applyZoneId);
        infoZone.setIsDelete("1");
        infoZone.setUpdateTime(TimeHelper.dateToLongString());
        infoZoneMapper.updateByIdSelective(infoZone);
        // 删除限时时段表
        infoLimitPeriodMapper.deleteLimitPeriod(applyZoneId);
        pubLogMapper.saveNoramlLogInfo(userName, systemName, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("删除infoZone表单编号:{%s} 操作成功!", new Object[] { infoZone.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.APPLY_REDION_MANAGEMENT.getMessage());
        // 更新无通行证redis
        List<InfoAlarmRedisAll> alarmRedisAll = infoAlarmRedisMapper.getAlarmRedisAll();
        redisTool.setList(noPassKey, alarmRedisAll);
        // 更新所有通行证redis
        List<PassCard> passTrucksList = passCardMapper.getAllPassTrucks();
        rltBayonetZoneService.setRedisByCardList(passTrucksList);
    }

    /**
     * 绘制地图区域信息
     */
    @Override
    public void drawingZoneShape(InfoZone infoZone, String userName) {
        infoZone.setUpdateTime(TimeHelper.dateToLongString());
        infoZoneMapper.drawingZoneShape(infoZone);
        pubLogMapper.saveNoramlLogInfo(userName, systemName, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
                String.format("绘制区域地理信息编号:{%s} 操作成功!", new Object[] { infoZone.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.APPLY_REDION_MANAGEMENT.getMessage());
    }

}
